<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msds="http://ehs-info.com/msds" xmlns="http://matsadaml.org/msds/v1.0" xmlns:ecinv="urn:eu.eca.reach:ecInventory" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" exclude-result-prefixes="msds ecinv xhtml #default">
	<xsl:param name="RiskPhrases_v0" select="document('v0_ResourceFiles/RiskPhrases.xml')/msds:riskPhrases"/>
	<xsl:param name="SafetyPhrases_v0" select="document('v0_ResourceFiles/SafetyPhrases.xml')/msds:safetyPhrases"/>
	<xsl:param name="Hazards_v0" select="document('v0_ResourceFiles/Hazards.xml')/msds:hazards"/>
	<xsl:template match="/">
		<xsl:processing-instruction name="xml-stylesheet">
			<xsl:text>type="text/xsl" href="../../System/Transformations/SubstanceIndex.xsl"</xsl:text>
		</xsl:processing-instruction>
		<xsl:apply-templates select="msds:substanceDB"/>
	</xsl:template>
	<xsl:template match="msds:substanceDB">
		<substanceDB xmlns="http://ehs-info.com/msds/v1.0" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ehs-info.com/msds/v1.0 ../System/Schema/msds_v1.0.xsd">
			<xsl:apply-templates/>
		</substanceDB>
	</xsl:template>
	<xsl:template match="msds:substance">
		<substance id="{@id}">
			<cas-number>
				<xsl:value-of select="msds:cas-number"/>
			</cas-number>
			<eg-number>
				<xsl:value-of select="msds:eg-number"/>
			</eg-number>
			<molecularFormula/>
			<names>
				<xsl:for-each select="msds:name">
					<name lang="{./@lang}">
						<xsl:value-of select="."/>
					</name>
				</xsl:for-each>
			</names>
			<descriptions/>
			<hazardCodes>
				<xsl:apply-templates select="msds:hazardCodes/msds:hazardCode"/>
			</hazardCodes>
			<riskPhrases>
				<xsl:apply-templates select="msds:riskPhrases/msds:riskPhrase"/>
			</riskPhrases>
			<exposureLimit>
				<xsl:if test="msds:exposureLimit/msds:ngv">
					<item name="nivågränsvärde" countries="SE; " unit="ppm">
						<xsl:value-of select="msds:exposureLimit/msds:ngv/msds:ppm"/>
					</item>
					<item name="nivågränsvärde" countries="SE; " unit="mgm3">
						<xsl:value-of select="msds:exposureLimit/msds:ngv/msds:mgm3"/>
					</item>
				</xsl:if>
				<xsl:if test="msds:exposureLimit/msds:tgv">
					<item name="takgränsvärde" countries="SE; " unit="ppm">
						<xsl:value-of select="msds:exposureLimit/msds:tgv/msds:ppm"/>
					</item>
					<item name="takgränsvärde" countries="SE; " unit="mgm3">
						<xsl:value-of select="msds:exposureLimit/msds:tgv/msds:mgm3"/>
					</item>
				</xsl:if>
				<xsl:if test="msds:exposureLimit/msds:ktv">
					<item name="korttidsvärde" countries="SE; " unit="ppm">
						<xsl:value-of select="msds:exposureLimit/msds:ktv/msds:ppm"/>
					</item>
					<item name="korttidsvärde" countries="SE; " unit="mgm3">
						<xsl:value-of select="msds:exposureLimit/msds:ktv/msds:mgm3"/>
					</item>
				</xsl:if>
				<xsl:if test="msds:exposureLimit/msds:year">
					<item name="year" countries="SE; ">
						<xsl:value-of select="msds:exposureLimit/msds:year"/>
					</item>
				</xsl:if>
				<xsl:if test="msds:exposureLimit/msds:remark">
					<item name="remark" countries="SE; ">
						<xsl:value-of select="msds:exposureLimit/msds:remark"/>
					</item>
				</xsl:if>
			</exposureLimit>
		</substance>
	</xsl:template>
	<xsl:template match="msds:riskPhrase | msds:safetyPhrase | msds:hazardCode">
		<xsl:element name="{local-name(.)}">
			<xsl:variable name="Code">
				<xsl:choose>
					<xsl:when test="local-name(.) = 'riskPhrase'">
						<xsl:value-of select="$RiskPhrases_v0/msds:riskPhrase[@id = current()/@id]/msds:code"/>
					</xsl:when>
					<xsl:when test="local-name(.) = 'safetyPhrase'">
						<xsl:value-of select="$SafetyPhrases_v0/msds:safetyPhrase[@id = current()/@id]/msds:code"/>
					</xsl:when>
					<xsl:when test="local-name(.) = 'hazardCode'">
						<xsl:value-of select="$Hazards_v0/msds:hazard[@id = current()/@id]/msds:code"/>
					</xsl:when>
				</xsl:choose>
			</xsl:variable>
			<xsl:choose>
				<xsl:when test="$Code">
					<xsl:attribute name="key"><xsl:value-of select="$Code"/></xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<xsl:attribute name="key"><xsl:value-of select="@id"/></xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
	</xsl:template>
	<xsl:template match="msds:riskPhrases | msds:safetyPhrases | msds:hazardCodes">
		<xsl:if test="* and not(*[@id = ''])">
			<xsl:apply-templates/>
		</xsl:if>
	</xsl:template>
	<xsl:template match="msds:substance/@id">
		<xsl:choose>
			<xsl:when test="../msds:cas-number">
				<xsl:attribute name="id"><xsl:value-of select="../msds:cas-number"/></xsl:attribute>
			</xsl:when>
			<xsl:when test="../msds:eg-number">
				<xsl:attribute name="id"><xsl:value-of select="../msds:eg-number"/></xsl:attribute>
			</xsl:when>
			<xsl:otherwise>
				<xsl:attribute name="id"><xsl:value-of select="."/></xsl:attribute>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="node()">
		<xsl:choose>
			<xsl:when test="local-name()">
				<xsl:element name="{local-name()}">
					<xsl:apply-templates select="@* | node() | text()"/>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="."/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<xsl:template match="@*">
		<xsl:copy/>
	</xsl:template>
</xsl:stylesheet>
